#!/bin/bash
shutdown()
{
echo "shuting...down";
exit;
}

trap shutdown 5 6 7 8 9
ingested=1
while :
do
{
minute=`date "+%M"`
if [ "$minute" = "03" ]; then 
	{  
		if [ "$ingested" = "0" ]; then
        		{
        		ingest_day=`date -d "1 hour ago" "+%d"`
        		ingest_month=`date -d "1 hour ago" "+%m"`
        		ingest_year=`date -d "1 hour ago" "+%Y"` 
        		ingest_hour=`date -d "1 hour ago" "+%H"` 
        		new_ingest_day=`date "+%d"`
        		new_ingest_month=`date "+%m"`
        		new_ingest_year=`date "+%Y"` 
        		new_ingest_hour=`date "+%H"`
        		
        		
        		/bin/bash sys_resources/ingest.sh $ingest_hour $ingest_day $ingest_month $ingest_year 1> /dev/null 2> /dev/null
        		
        		wait;
        		/bin/bash sys_resources/ingest.sh $new_ingest_hour $new_ingest_day $new_ingest_month $new_ingest_year 1> /dev/null 2> /dev/null
        		sleep 10;   
        		extract_day=`date -d "3 hour ago" "+%d"`
        		extract_month=`date -d "3 hour ago" "+%m"`
        		extract_year=`date -d "3 hour ago" "+%Y"` 
        		extract_hour=`date -d "3 hour ago" "+%H"`
        		bash sys_resources/dmsout.sh "$extract_year-$extract_month-$extract_day $extract_hour:00:00" "$new_ingest_year-$new_ingest_month-$new_ingest_day $new_ingest_hour:00:00" 1>/dev/null 2>/dev/null 
        		#Call to backpoll program
        		bash sys_resources/backpoll.sh &> /dev/null 
        		#/bin/bash sys_resources/spmout.sh $ingest_hour $ingest_day $ingest_month $ingest_year
        		wait;
        		sleep 10;
			if (stat *.MIN &> /dev/null); then
			 {
			for i in `ls *.MIN`
				do
				{
					bash sys_resources/carbput.sh $i 1>/dev/null 2>/dev/null
				}
				done
			 }
			fi
			ingested=1
        		}
 		fi
 		sleep 60;
 	}
 else
 	{
 	sleep 4;
 	ingested=0
 	}
 fi
 
 }
 done
 
